<template>
  <div class="mlt">
    <div style="margin-bottom: 10px">
      <el-button
        v-permissions="{ permission: ['add:InventoryType'] }"
        type="primary"
        @click="addType"
      >
        新增盘点类型
      </el-button>
    </div>
    <div>
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="类型名称" prop="type_name" />
        <el-table-column label="操作" prop="address">
          <template slot-scope="scoped">
            <!-- <el-button type="text" @click="delinv(scoped.row)">删除</el-button> -->
            <el-button
              v-permissions="{ permission: ['edit:InventoryType'] }"
              type="primary"
              @click="addType(scoped.row)"
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="盘点类型" :visible.sync="dialogVisible" width="30%">
      <span>类型名称：</span>
      <el-input
        v-model="form.type_name"
        placeholder="请输入类型名称"
        style="width: 200px"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sbumit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { invTypeList, invTypeAdd } from '@/api/commodity'
  export default {
    data() {
      return {
        emptyText: ' ',
        tableData: [],
        dialogVisible: false,
        form: {
          type_name: '',
        },
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        invTypeList().then((e) => {
          this.tableData = e.data
          loading.close()
          if (e.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },

      addType(e) {
        if (e.id) {
          console.log(e)
          this.form.id = e.id
          this.form.type_name = e.type_name
        } else {
          this.form = {
            type_name: '',
          }
        }
        this.dialogVisible = true
      },

      sbumit() {
        invTypeAdd(this.form).then(() => {
          this.init()
        })
        this.dialogVisible = false
      },
    },
  }
</script>

<style></style>
